﻿using System;



namespace YinLongTool.Number
{
    public static class LongUtil
    {
      

        /// <summary>
        /// 转换成字节数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] GetBytes( long value)
        {
            return BitConverter.GetBytes(value);
        }

        /// <summary>
        /// 字符串转long
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <returns>int类型的数字</returns>
        public static long ToInt64(string s)
        {
            Int64.TryParse(s, out var result);
            return result;
        }


        /// <summary>
        /// 字符串转long
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns>int类型的数字</returns>
        public static long ToInt64(string s, int defaultValue = 0)
        {
            return long.TryParse(s, out var result) ? result : defaultValue;
        }
        // /// <summary>
        // /// 字符串转long类型
        // /// </summary>
        // /// <param name="str"></param>
        // /// <param name="defaultResult">转换失败的默认值</param>
        // /// <returns></returns>
        // public static long ToLong(this string str, long defaultResult)
        // {
        //     if (!Int64.TryParse(str, out var result))
        //     {
        //         result = defaultResult;
        //     }
        //     return result;
        // }  
        /// <summary>
        /// 字符串转long类型
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultResult">转换失败的默认值</param>
        /// <returns></returns>
        public static long ToLong(string str, long defaultResult = 0)
        {
            return long.TryParse(str, out var result) ? result : defaultResult;
        }
    }
}